import router from './router'
import { ElMessage } from 'element-plus'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import { isHttp, isPathMatch } from '@/utils/validate'
import { isRelogin } from '@/utils/request'
import useUserStore from '@/store/modules/user'

NProgress.configure({ showSpinner: false })

// const userStore = useUserStore();
const whiteList = ['/login', '/register','/phoneLogin','/miniAppLogin']

const isWhiteList = (path) => {
  return whiteList.some(pattern => isPathMatch(pattern, path))
}

router.beforeEach( (to, from, next) => {
  NProgress.start()
  if (isWhiteList(to.path)) {
    // 在免登录白名单，直接进入
    next()
  } else {
    useUserStore().getInfo().then((user) => {
        if(user.id!==''&&user.name!==''){
            next()
        }else{
            ElMessage.error('用户信息获取失败，请重新登录')
            next(`/login`)
        }
        NProgress.done()
    }).catch(() => {
        next(`/login`)
        ElMessage.error('用户信息获取失败，请重新登录')
        NProgress.done()
    })
    NProgress.done()
  }
  
})